﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Karlsbad.Channels;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Karlsbad.Controllers
{
    [Route("pac-active")]
    [ApiController]
    public class PacActiveController : ControllerBase
    {
        public static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
        private static IProducer _producer;

        public PacActiveController(IProducer producer)
        {
            _producer = producer;
        }

        // POST api/<PacActiveController>
        [HttpPost]
        public async Task<ContentResult> Index()
        {
            ContentResult rm = new ContentResult();
            rm.ContentType = "application/json;charset=utf-8";
            rm.StatusCode = 200;
            string result = string.Empty;
            using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                result = await reader.ReadToEndAsync();
            }
            if (!string.IsNullOrEmpty(result))
            {
                log.Info("接收到报文：" + result);
                await _producer.SendAsync(result);
            }
            rm.Content = "success";
            return rm;
        }

    }
}
